package com.cotte.flow.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cotte.flow.model.DgraphRelations;
import com.cotte.flow.model.DgraphSteps;
import com.cotte.flow.service.OrderStepDgraphService;
import com.cotte.flow.service.StepDgraphService;

import net.sf.json.JSONArray;

@RestController
@RequestMapping("/order/step")
public class OrderStepDgraphController {
	
	@Autowired
	private OrderStepDgraphService orderStepDgraphService;
	
	@Autowired
	private StepDgraphService service;
	
	@Autowired
	private StepDgraphService stepDgraphService;
	
	@Value("${dgraph.order.steps.name.prefix}")
	private String ORDER_STEP_NAME;
	
	@Value("${dgraph.order.relations.name.prefix}")
	private String ORDER_RELATION_NAME;
	
	@Value("${dgraph.steps.name.prefix}")
	private String STEP_NAME;
	@Value("${dgraph.relations.name.prefix}")
	private String RELATION_NAME;
	
	/**
	 * 创建订单工序流
	 * <p>Title: orderCreate</p>  
	 * <p>Description: </p>  
	 * @return
	 * @throws Exception 
	 */
	@GetMapping("/create")
	public String orderCreate(String version) throws Exception {
		String syscode = "USI812050047";
		String depart = "F001";
		String cate = "MXF";
		String createTime = "20181205095700";
		orderStepDgraphService.orderCreate(version,syscode, depart, cate,createTime);
		return "success";
	}
	/**
	 * 查询订单工序流
	 * <p>Title: query</p>  
	 * <p>Description: </p>  
	 * @return
	 */
	@GetMapping("/query")
	public Map<String,JSONArray> query(String version) {
		Map<String, JSONArray> result = new HashMap<String, JSONArray>();
		try {
			String stepName = ORDER_STEP_NAME+version+"_"+"CU1812010608_11428031";
			String relationName = ORDER_RELATION_NAME+version+"_"+"CU1812010608_11428031";
			DgraphSteps steps = stepDgraphService.getDgraphSteps(stepName);
			JSONArray steps_arr = JSONArray.fromObject(steps.getSteps());
			DgraphRelations relations = stepDgraphService.getDgraphRelation(relationName);
			JSONArray rel_arr = JSONArray.fromObject(relations.getRelations());
			result.put("nodeDataArray", steps_arr);
			result.put("linkDataArray", rel_arr);
			System.out.println(rel_arr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 
	 * <p>Title: query</p>  
	 * <p>Description: </p>  
	 * @param version
	 * @return
	 */
	@GetMapping("/queryNew")
	public Map<String,Object> queryNew(String version) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			//订单的step
			String stepName = ORDER_STEP_NAME+version+"_"+"USI812050047_11427861";
			
			DgraphSteps steps = service.getDgraphSteps(stepName);
			//将odid为空的去掉只保留odid有值的节点
		/*	List<Step> newSteps = new ArrayList<>();
			for(Step st:steps.getSteps()) {
				if(st.getisGroup() || !StringUtils.isEmpty(st.getOdid())) {
					newSteps.add(st);
				}
			}*/
			JSONArray steps_arr = JSONArray.fromObject(steps.getSteps());
			String parentStep = steps.getParentStep();
			//订单的relation
			String relationName = ORDER_RELATION_NAME+version+"_"+"USI812050047_11427861";
			DgraphRelations relations = service.getDgraphRelation(relationName);
			//List<Relation> rels = service.checkOccupy(relations.getRelations());
			JSONArray rel_arr = JSONArray.fromObject(relations.getRelations());
			//总图的Link和relation
			String parentRelation = RELATION_NAME+parentStep;
			DgraphRelations parentRels = service.getDgraphRelation(parentRelation);
			JSONArray parent_rel_arr = JSONArray.fromObject(parentRels.getRelations());
			JSONArray linkDataArray = null;
			if(parentRels.getLinks() == null) {
				linkDataArray = JSONArray.fromObject("[]");
			}else {
				linkDataArray = JSONArray.fromObject(parentRels.getLinks());
			}
			Map<String,Map<String,List<String>>> map = service.fzMap();
			result.put("nodeDataArray", steps_arr);
			result.put("linkRel", parent_rel_arr);
			result.put("linkOrderRel", rel_arr);
			result.put("linkDataArray", linkDataArray);
			result.put("fzRel", map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
}	
